package com.sanjishu.enterlab.electricity.sale.service;

import com.sanjishu.enterlab.electricity.sale.domain.MonthElecData;
import com.sanjishu.enterlab.electricity.sale.domain.MonthUserData;
import com.sanjishu.enterlab.electricity.sale.mapper.MonthElecDataMapper;
import com.sanjishu.enterlab.electricity.sale.mapper.MonthUserDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MonthUserDataService {

    @Autowired
    private MonthElecDataMapper monthElecDataMapper;
    @Autowired
    private MonthUserDataMapper monthUserDataMapper;

    public void updateUserDatas(Integer year, Integer month) {
        List<MonthElecData> monthElecDataList = monthElecDataMapper.listAllByMonth(year, month);
        Map<Integer, List<BigDecimal>> userMap = new HashMap<>();
        if (monthElecDataList != null && monthElecDataList.size() > 0) {
            for (MonthElecData monthElecData : monthElecDataList) {
                List<BigDecimal> dataList = userMap.get(monthElecData.getUserId());
                //第一个记录预估电量总计，第二个记录实际电量总计，第三个记录代理费
                if (dataList == null) {
                    dataList = new ArrayList<>();
                    dataList.add(monthElecData.getElecConsumption());
                    dataList.add(monthElecData.getElecActual());
                    dataList.add(monthElecData.getAgencyFee());
                } else {
                    BigDecimal elecConsumption = monthElecData.getElecConsumption().add(dataList.get(0));
                    BigDecimal elecActual = monthElecData.getElecActual().add(dataList.get(1));
                    BigDecimal agencyFee = monthElecData.getAgencyFee().add(dataList.get(2));
                    dataList.set(0, elecConsumption);
                    dataList.set(1, elecActual);
                    dataList.set(2, agencyFee);
                }
                userMap.put(monthElecData.getUserId(), dataList);
            }

            if(userMap != null && userMap.values().size()>0) {
                for (Integer userId : userMap.keySet()) {
                    List<BigDecimal> currentDataList = userMap.get(userId);
                    if (currentDataList != null && currentDataList.size()==3) {
                        BigDecimal diff = currentDataList.get(0).subtract(currentDataList.get(1));
                        BigDecimal elecDeviation = diff.divide(currentDataList.get(0), 2, BigDecimal.ROUND_HALF_UP);
                        MonthUserData monthUserData = monthUserDataMapper.findByUserAndMonth(userId, year, month);
                        if (monthUserData != null) {
                            updateMonthUserData(monthUserData, userId, month, year, elecDeviation, currentDataList.get(2));
                        } else {
                            createMonthUserData(userId, month, year, elecDeviation, currentDataList.get(2));
                        }
                    }
                }
            }
        }
    }



    public void createMonthUserData(Integer userId, Integer month, Integer year, BigDecimal elecDeviation,
                                    BigDecimal agenceFee) {
        MonthUserData monthUserData = new MonthUserData();
        monthUserData = setData(monthUserData, userId, month, year, elecDeviation, agenceFee);
        monthUserData.setCreatedAt(new Date());
        monthUserDataMapper.insert(monthUserData);
    }

    public void updateMonthUserData(MonthUserData monthUserData, Integer userId, Integer month, Integer year, BigDecimal elecDeviation,
                                    BigDecimal agenceFee) {
        if (monthUserData != null) {
            monthUserData = setData(monthUserData, userId, month, year, elecDeviation, agenceFee);
            monthUserData.setUpdatedAt(new Date());
            monthUserDataMapper.updateByPrimaryKey(monthUserData);
        }
    }


    private MonthUserData setData(MonthUserData monthUserData, Integer userId, Integer month,
                                 Integer year, BigDecimal elecDeviation,
                                 BigDecimal agenceFee) {
        monthUserData.setUserId(userId);
        monthUserData.setYear(year);
        monthUserData.setMonth(month);
        monthUserData.setElecDeviation(elecDeviation);
        monthUserData.setAgencyFee(agenceFee);
        monthUserData.setUpdatedAt(new Date());
        return monthUserData;
    }
}
